﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using Tesla.Content;

namespace Tesla.Graphics {
    /// <summary>
    /// A material collection that represents a complete set of materials in a TEM file, in the
    /// order they appear in the file.
    /// </summary>
    public sealed class MaterialCollection : IEnumerable<Material>, ISavable {
        private String _name;
        private List<Material> _materials;

        /// <summary>
        /// Gets the number of materials contained in the collection.
        /// </summary>
        public int Count {
            get {
                return _materials.Count;
            }
        }

        /// <summary>
        /// Gets or sets the name of the collection.
        /// </summary>
        public String Name {
            get {
                return _name;
            }
            set {
                _name = value;
            }
        }

        /// <summary>
        /// Gets a material in the collection based on a zero-based index.
        /// </summary>
        /// <param name="index">Index of the material</param>
        /// <returns>The material, if it exists</returns>
        public Material this[int index] {
            get {
                if(index >= 0 && index < _materials.Count) {
                    return _materials[index];
                }
                return null;
            }
        }

        /// <summary>
        /// Gets a material in the collection based on its name. If there
        /// are multiple materials in the collection with the same name, the first
        /// material is returned.
        /// </summary>
        /// <param name="name">Name of the material</param>
        /// <returns>The material, if it exists</returns>
        public Material this[String name] {
            get {
                for(int i = 0; i < _materials.Count; i++) {
                    Material mat = _materials[i];
                    String matName = mat.Name;
                    if(matName != null && matName.Equals(name)) {
                        return mat;
                    }
                }
                return null;
            }
        }

        /// <summary>
        /// Creates a new MaterialCollection.
        /// </summary>
        public MaterialCollection() {
            _name = "Materials";
            _materials = new List<Material>(1);
        }

        /// <summary>
        /// Creates a new MaterialCollection.
        /// </summary>
        /// <param name="name">Name of the material collection</param>
        public MaterialCollection(String name) {
            _name = name;
            _materials = new List<Material>(1);
        }

        /// <summary>
        /// Adds a new material to the collection.
        /// </summary>
        /// <param name="material">Material to be added</param>
        public void Add(Material material) {
            if(material != null) {
                _materials.Add(material);
            }
        }

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
        /// </returns>
        public IEnumerator<Material> GetEnumerator() {
            return _materials.GetEnumerator();
        }

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
            return _materials.GetEnumerator();
        }

        /// <summary>
        /// Serializes the object and writes it to the output.
        /// </summary>
        /// <param name="output">Savable Output</param>
        public void Write(ISavableWriter output) {
            output.Write("Name", _name);
            output.WriteSavable<Material>("Materials", _materials.ToArray());
        }

        /// <summary>
        /// Deserializes the object and populates it from the input.
        /// </summary>
        /// <param name="input">Savable input</param>
        public void Read(ISavableReader input) {
            _name = input.ReadString();
            _materials.AddRange(input.ReadSavableArray<Material>());
        }
    }
}
